package Groups;

sub new 
{
    my ($class) = @_;
    my $self    = {};

    $self->{active}         = "0,1";
    $self->{group_name}     = "";
    $self->{group_id}       = 1;
    $self->{recipient_id}   = 0;
    $self->{realm_id}       = 0;

    bless $self, 'Groups';
    return $self;
}

sub newFromDB
{
    my ($class, $group_id)	= @_;
    my $self		= {};

    my $stmt	= "SELECT id, group_name, active, realm_id FROM groups WHERE id = $group_id";
    
    my $kit_db  = new KIT_DB();
    
    my $hashref	= $kit_db->GetSingleRow($stmt);

    $self->{active}		= "0,1";
    $self->{group_name}	= $hashref->{'group_name'};
    $self->{group_id}	= $group_id;
    $self->{realm_id}	= $hashref->{'realm_id'};

    #	-- Get number of active addresses in group
    my $stmt	= "SELECT count(*) AS active_count FROM recipients_groups AS RG, recipients AS R WHERE R.active = 1 AND group_id = $group_id AND R.id = RG.recipient_id AND type_id = 2";
    my $hashref	= $kit_db->GetSingleRow($stmt);

    $self->{active_count}	= $hashref->{'active_count'};

    #	-- Get number of inactive addresses in group
    my $stmt	= "SELECT count(*) AS inactive_count FROM recipients_groups AS RG, recipients AS R WHERE R.active = 0 AND group_id = $group_id AND R.id = RG.recipient_id AND type_id = 2";
    my $hashref	= $kit_db->GetSingleRow($stmt);

    $self->{inactive_count}	= $hashref->{'inactive_count'};

    bless $self, 'Groups';
    return $self;
}

sub InsertGroup 
{
    my $self	= shift;
    my $stmt	= "INSERT INTO groups (group_name, realm_id) VALUES (\'$self->{group_name}\', $self->{realm_id})";

    my $kit_db  = new KIT_DB();
    return $kit_db->Insert($stmt);
}

sub UpdateGroup 
{
    my $self	= shift;
    my $stmt	= "UPDATE groups SET group_name = \'$self->{group_name}\', active = \'$self->{active}\' WHERE id = $self->{group_id}";
    # print $stmt; exit;
    my $kit_db  = new KIT_DB();
    return $kit_db->Update($stmt);
}

sub UpdateGroupRecipient
{
    my ($self, $recipient_id, $recipient_group_id)	= @_;
    my $stmt	= "UPDATE recipients_groups SET group_id = $self->{group_id} WHERE id = $recipient_group_id AND recipient_id = $recipient_id";
    $Utils::logger->info($stmt);
    my $kit_db  = new KIT_DB();
    return $kit_db->Update($stmt);
}

#  -- Gets all groups, with details such as g_id, g_name
sub GetAllGroups
{
    my $self	      = shift;
    my $active	      = shift;# || "1";
    my $with_totals	= shift || "0";

    # print "active = " . $active . "<br>";
    # 	$active	= "0,1" if ($active != 0 || $active != 1);

    my $kit_db  = new KIT_DB();

    my @allGroups	= ();
    my ($total_real_addr, $total_test_addr, $total_campaigns) = (0, 0, 0);

    my $stmt	= "SELECT id, group_name, active FROM groups WHERE active IN ($active) AND realm_id = " . $self->{'realm_id'};
    my @rows	= $kit_db->GetMultipleRows($stmt);
#     print $stmt; # exit;

    foreach (@rows)
    {
        my $group_id		= $_->[0];
        my $group_name		= $_->[1];
        my $group_address_count_real	= $self->GetRecipientCount($group_id, 2);
        my $group_address_count_test	= $self->GetRecipientCount($group_id, 1);

        my $group_campaign_count= $self->GetCampaignCount($group_id);

        my %group	= (
                group_id                    => $group_id,
                group_name                  => $group_name,
                group_address_count_real    => $group_address_count_real,
                group_address_count_test    => $group_address_count_test,
                group_campaign_count        => $group_campaign_count,
                realm_id                    => $_->[3],
                active_groups               => $active,
        );
        push @allGroups, \%group;

        #	-- Totals
        $total_real_addr += $group_address_count_real;
        $total_test_addr += $group_address_count_test;
        $total_campaigns += $group_campaign_count;
    }

    if ($with_totals) {
        push @allGroups, $total_real_addr;
        push @allGroups, $total_test_addr;
        push @allGroups, $total_campaigns;
    }

    return @allGroups;
}

#	-- Returns active recipient by default
sub GetRecipientCount
{
    my $self	= shift;
    my $group_id	= shift || $self->{group_id};
    my $recp_type_id= shift || 2;
    my $active	= shift || 1;

    my $stmt	= "SELECT COUNT(*) AS ret_val 
                    FROM   recipients_groups AS RG, recipients AS R 
                    WHERE  RG.group_id = $group_id
                    AND	  R.type_id = $recp_type_id
                    AND	  RG.recipient_id = R.id
                    AND	  realm_id = $self->{realm_id}
                    AND	  RG.active IN ($active)";

    my $kit_db  = new KIT_DB();
    return $kit_db->GetSingleValue($stmt);
}

sub get_recipients
{
    my $self	= shift;
    my $group_id	= shift || $self->{group_id};
    my $recp_type_id= shift || 2;
    my $active	= shift || 1;

    my $stmt	= "SELECT R.id AS ret_val 
                    FROM   recipients_groups AS RG, recipients AS R 
                    WHERE  RG.group_id = $group_id
                    AND	  R.type_id = $recp_type_id
                    AND	  RG.recipient_id = R.id
                    AND	  realm_id = $self->{realm_id}
                    AND	  RG.active IN ($active)";

    my $kit_db  = new KIT_DB();
    return $kit_db->GetMultipleRows($stmt);
}


sub GetCampaignCount
{
	my $self	= shift;
	my $group_id	= shift || $self->{group_id};
	my $realm_id	= $self->{realm_id};

	my $stmt	= "SELECT COUNT(*) AS ret_val 
			   FROM   campaigns AS CP, groups AS G, content AS C
			   WHERE  group_id = $group_id
			   AND 	  G.id = CP.group_id 
			   AND	  C.id = CP.content_id
			   AND 	  G.realm_id = $realm_id
			   AND	  C.realm_id = $realm_id" ;

    my $kit_db  = new KIT_DB();
    return $kit_db->GetSingleValue($stmt);
}

#	-- Inserts a mapping record for recipient + group
sub InsertRecipientGroup
{
    my ($self)  = @_;

    my $stmt    = "INSERT INTO recipients_groups (recipient_id, group_id, active) VALUES ($self->{recipient_id}, $self->{group_id}, $self->{active})";
    my $kit_db  = new KIT_DB();

    return $kit_db->Insert($stmt);
}

sub get_count_with_lname {
    my $self    = shift;
    my $stmt    = "SELECT count(*) AS ret_val FROM recipients_groups AS rg, recipients AS r 
                   WHERE  rg.recipient_id = r.id 
                   AND    (r.l_name != '' OR r.l_name != NULL)
                   AND    rg.active = 1
                   AND    r.type_id = 2
                   AND    r.realm_id = $self->{realm_id}
                   AND    rg.group_id = " . $self->{group_id};

    my $kit_db  = new KIT_DB();
    return $kit_db->GetSingleValue($stmt);

}

sub get_count_with_fname {
    my $self    = shift;
    my $stmt    = "SELECT count(*) AS ret_val FROM recipients_groups AS rg, recipients AS r 
                   WHERE  rg.recipient_id = r.id 
                   AND    r.f_name != ''
                   AND    rg.active = 1
                   AND    r.type_id = 2
                   AND    r.realm_id = $self->{realm_id}
                   AND    rg.group_id = " . $self->{group_id};

    my $kit_db  = new KIT_DB();
    return $kit_db->GetSingleValue($stmt);
}

sub get_count_with_salutation {
    my $self    = shift;
    my $stmt    = "SELECT count(*) AS ret_val FROM recipients_groups AS rg, recipients AS r 
                   WHERE  rg.recipient_id = r.id 
                   AND    r.salutation_id != 0 
                   AND    rg.active = 1
                   AND    r.type_id = 2
                   AND    r.realm_id = $self->{realm_id}
                   AND    rg.group_id = " . $self->{group_id};

    my $kit_db  = new KIT_DB();
    return $kit_db->GetSingleValue($stmt);
}

sub get_count_with_email {
    my $self    = shift;
    my $stmt    = "SELECT count(*) AS ret_val FROM recipients_groups AS rg, recipients AS r 
                   WHERE  rg.recipient_id = r.id 
                   AND    (r.email_addr != 'NULL' AND r.email_addr != '')
                   AND    rg.active = 1
                   AND    r.type_id = 2
                   AND    r.realm_id = $self->{realm_id}
                   AND    rg.group_id = " . $self->{group_id};

# printf "<p>%s</p>", $stmt;
    my $kit_db  = new KIT_DB();
    return $kit_db->GetSingleValue($stmt);
}

sub get_count_with_companyname {
    my $self    = shift;
    my $stmt    = "SELECT count(*) AS ret_val FROM recipients_groups AS rg, recipients AS r 
                   WHERE  rg.recipient_id = r.id 
                   AND    (r.company_name != 'NULL' AND r.company_name != '')
                   AND    rg.active = 1
                   AND    r.type_id = 2
                   AND    r.realm_id = $self->{realm_id}
                   AND    rg.group_id = " . $self->{group_id};

    my $kit_db  = new KIT_DB();
    return $kit_db->GetSingleValue($stmt);
}

sub get_count_with_companynameshort {
    my $self    = shift;
    my $stmt    = "SELECT count(*) AS ret_val FROM recipients_groups AS rg, recipients AS r 
                   WHERE  rg.recipient_id = r.id 
                   AND    (r.company_name_short != 'NULL' AND r.company_name_short != '')
                   AND    rg.active = 1
                   AND    r.type_id = 2
                   AND    r.realm_id = $self->{realm_id}
                   AND    rg.group_id = " . $self->{group_id};

    my $kit_db  = new KIT_DB();
    return $kit_db->GetSingleValue($stmt);
}

sub delete
{
    my $self    = shift;
    my $sql     = "DELETE FROM groups WHERE id = " . $self->{group_id};
#     print "\nDeleting new group with ID: $new_group_id\n";
    my $kit_db  = new KIT_DB();
    $kit_db->Delete($sql);
    return;
}


1;